//index.js
//获取应用实例
var app = getApp();
let httpUrl = app.globalData.httpUrl;
let thisErpId = app.globalData.thisErpId;
Page({
  data: {
    searchType: 'huangliao',
    searchPageType: '1',
    companyName: '',
    stoneName: '',
    stoneImg: '',
    imgHttpUrl: 'http://www.stonhub.com/',
    hipperPhone: '',
    stoneData: [],
    rightUnit: 'm³',
    stoneVolume: '',
    stoneWeight: '',
    stoneSum: '',
    sunUnit: '颗',
    volUnit: '体　积',
  },
  onLoad: function (e) {
    let objData = wx.getStorageSync('searchObj');
    let searchPageType = objData.searchPageType;
    let searchType = (searchPageType == 1) ? 'huangliao' : 'daban';
    let rightUnit = (searchPageType == '1') ? 'm³' : 'm²';
    let sunUnit = (searchPageType == '1') ? '颗' : '匝';
    let volUnit = (searchPageType == '1') ? '体　积' : '面　积';
    let companyName = objData.companyName;
    let stoneName = objData.stoneName;
    let stoneImg = objData.stoneImg;
    this.setData({
      searchType: searchType,
      searchPageType: searchPageType,
      companyName: companyName,
      stoneName: stoneName,
      stoneImg: stoneImg,
      rightUnit: rightUnit,
      sunUnit: sunUnit,
      volUnit: volUnit,
    })
    if (wx.showLoading) {
      wx.showLoading({
        title: '正在加载...',
      })
    }
    this.getStoneMessage(objData);
  },
  getStoneMessage: function (objData) {
    let obj = this.data;
    let searchType = obj.searchType;
    let companyName = obj.companyName;
    let stoneName = obj.stoneName;
    //石材的长宽高
    let widthFilter = objData.widthFilter || -1;
    let heightFilter = objData.heightFilter || -1;
    let lengthFilter = objData.lengthFilter || -1;
    let volumeFilter = objData.volumeFilter || -1;
    let widthSum = objData.widthSum || -1;
    let heightSum = objData.heightSum || -1;
    let lengthSum = objData.lengthSum || -1;
    let volumeSum = objData.volumeSum || -1;
    if (searchType == 'daban') {
      volumeFilter = null;
      volumeSum = null;
    }
    wx.request({
      url: httpUrl + 'slsw/detailsearch.do',
      method: 'POST',
      data: `weChatKey=9237B361957490BA9644A710A5E3DD84&&Data={width_filter:${widthFilter},height_filter:${heightFilter},
      length_filter:${lengthFilter},volume_filter:${volumeFilter},width:${widthSum},height:${heightSum},length:${lengthSum},
      volume:${volumeSum},companyName:'${companyName}',stoneName:'${stoneName}',search_type:'${searchType}',userId:1,
      erpId:'${thisErpId}'}`,
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8"'
      },
      success: (res) => {
        console.log(res.data)
        let data = res.data.Data
        let stoneVolume = ((this.getSum(data, 'vol')) / 1000000000000).toFixed(3);
        let stoneWeight = ((this.getSum(data, 'wei')) / 1000000000000).toFixed(3);
        let stoneSum = this.getSum(data, 'stoneSum');
        this.setData({
          stoneData: data,
          hipperPhone: data[0].phone,
          stoneVolume: stoneVolume,
          stoneWeight: stoneWeight,
          stoneSum: stoneSum,
        })
        wx.hideLoading();
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '服务器开了个小差，请稍后再试!',
          icon: 'loading'
        })
      }
    })
  },
  calling: function (e) {
    let phone = this.data.hipperPhone;
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  //参数相加
  getSum: function (arr, para) {
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
      if (para == 'vol') {
        sum += parseFloat(arr[i].stoneVolume) * 1000000000000;
      } else if (para == 'wei') {
        console.log('777')
        sum += parseFloat(arr[i].stoneWeight) * 1000000000000;
      } else {
        sum = i + 1;
      }
    }
    return sum
  },
  // 货主排序图片预览
  shipPreimage: function (e) {
    console.log(e);
    let photos = e.currentTarget.dataset.urlarr;
    wx.previewImage({
      urls: photos // 需要预览的图片http链接列表  
    })
  },
  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {

  }
  // entrancesl: function (e) { 
  //   wx.showToast({
  //     title: '暂未开放!',
  //     icon: 'clear'
  //   })
  // },

})
